import redis
from common.config import *

class OperaRedisDb():
    def __init__(self,env):
        redis_host_db = getconf(env, "redis_host_db")
        redis_password = getconf(env, "redis_password")
        port = getconf(env, "port")
        try:
            self.conn = redis.Redis(host=redis_host_db,port=6379,password=redis_password,db=0)
            # self.cur = self.conn.cursor()
            print("redis连接成功")
        except Exception as e:
            print("redis连接失败|Error %s" % e)
    def del_redis(self,keys_list):
        for i in keys_list:
            data = self.conn.keys(i)
            count = 1
            for i in data:
                self.conn.delete(i)
                count+=1
            print("成功删除{}条数据".format(count))
        self.conn.close()
        print("redis连接已关闭")

    def set_redis(self,keys_list,value):
        for i in keys_list:
            data = self.conn.keys(i)
            count = 1
            for i in data:
                self.conn.set(i,value)
                count+=1
            print("成功更新{}条数据".format(count))
        self.conn.close()
        print("redis连接已关闭")

    def __del_(self):
        self.conn.close()
        print("redis连接已关闭")